// Filename: router.js
define([
    'jquery',
    'underscore',
    'backbone',
    'module/home/HomeView',
    'module/home/LoginView',
    'module/category/CategoryController',
    'module/article/ArticleController',
    'module/operator/OperatorController',
    'module/operation/OperationController',
    'module/user/UserController',
    'module/unit/UnitController',
    'module/brand/BrandController',
    'module/article/ListPriceController',
    'module/dashboard/DashBoardController',
    'module/user/UserInfoController',
    'module/config/ConfigController',
    'module/import/ImportController',
    'module/article/ReportArticleController',
    'module/operation/ReportOperationController',
    'module/charge/ChargeController',
    'module/header/HeaderController',
    'module/document_type/DocumentTypeController',
    'module/document_identity/DocumentIdentityController'
], function($, _, Backbone, HomeView, LoginView,
        CategoryController, ArticleController, OperatorController,
        OperationController, UserController, UnitController,
        BrandController, ListPriceController, DashBoardController,
        UserInfoController, ConfigController, ImportController,
        ReportArticleController, ReportOperationController, ChargeController, HeaderController,
        DocumentTypeController, DocumentIdentityController) {

    var AppRouter = Backbone.Router.extend({
        routes: {
            'category': 'category',
            'article': 'article',
            'supplier': 'supplier',
            'client': 'client',
            'purcharse': 'purcharse',
            'sale': 'sale',
            'unit': 'unit',
            'brand': 'brand',
            'listprice': 'listprice',
            'dashboard': 'dashboard',
            'userinfo': 'userinfo',
            'user': 'user',
            'config': 'config',
            'import': 'import',
            'charge': 'charge',
            'header_purcharse': 'header_purcharse',
            'header_sale': 'header_sale',
            'document_type': 'document_type',
            'document_identity': 'document_identity',
            //------------------------------------------------------------------
            'report_article_category': 'report_article_category',
            'report_article_valued': 'report_article_valued',
            'report_article_stock': 'report_article_stock',
            'report_purcharse': 'report_purcharse',
            'report_purcharse_supplier': 'report_purcharse_supplier',
            'report_sale': 'report_sale',
            'report_sale_client': 'report_sale_client',
            //------------------------------------------------------------------

            'login': 'login',
            'logout': 'logout',
            '*actions': 'defaultAction'// por defecto
        }
    });

    var initialize = function() {
        var app_router = new AppRouter;
        //----------------------------------------------------------------------
        app_router.on('route:report_purcharse', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ReportOperationController();
            controller.report_purcharse();
        });
        app_router.on('route:report_purcharse_supplier', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ReportOperationController();
            controller.report_purcharse_supplier();
        });
        app_router.on('route:report_sale', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ReportOperationController();
            controller.report_sale();
        });
        app_router.on('route:report_sale_client', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ReportOperationController();
            controller.report_sale_client();
        });
        //----------------------------------------------------------------------
        app_router.on('route:document_type', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new DocumentTypeController();
            controller.render();
        });
        app_router.on('route:document_identity', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new DocumentIdentityController();
            controller.render();
        });
        app_router.on('route:report_article_category', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ReportArticleController();
            controller.article_category();
        });

        app_router.on('route:import', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ImportController();
            controller.render();
        });

        app_router.on('route:report_article_valued', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ReportArticleController();
            controller.article_valued();
        });

        app_router.on('route:report_article_stock', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ReportArticleController();
            controller.article_stock();
        });
        //----------------------------------------------------------------------
        app_router.on('route:config', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ConfigController();
            controller.render();
        });
        app_router.on('route:charge', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ChargeController();
            controller.render();
        });
        app_router.on('route:userinfo', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new UserInfoController();
            controller.render();
        });
        app_router.on('route:dashboard', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new DashBoardController();
            controller.render();
        });
        app_router.on('route:category', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new CategoryController();
            controller.render();
        });
        app_router.on('route:listprice', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ListPriceController();
            controller.render();
        });

        app_router.on('route:unit', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new UnitController();
            controller.render();
        });

        app_router.on('route:brand', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new BrandController();
            controller.render();
        });

        app_router.on('route:user', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new UserController();
            controller.render();
        });

        app_router.on('route:article', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new ArticleController();
            controller.render();
        });

        app_router.on('route:supplier', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new OperatorController({title: "Proveedor", type: "1"});
            controller.render();
        });

        app_router.on('route:client', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new OperatorController({title: "Clientes", type: "2"});
            controller.render();
        });

        app_router.on('route:purcharse', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new OperationController({title: "Compras", type: "1", title_operator: "Proveedor"});
            controller.render();
        });

        app_router.on('route:sale', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new OperationController({title: "Ventas", type: "2", title_operator: "Clientes"});
            controller.render();
        });

        app_router.on('route:header_purcharse', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new HeaderController({title: "Compras", type: "1", title_operator: "Proveedor"});
            controller.render();
        });

        app_router.on('route:header_sale', function() {
            $("#page").find("*").andSelf().unbind();
            var controller = new HeaderController({title: "Ventas", type: "2", title_operator: "Clientes"});
            controller.render();
        });
//------------------------------------------------------------------------------
        app_router.on('route:logout', function(actions) {
            var view = new LoginView();
            view.logout();
        });

        app_router.on('route:login', function(actions) {
            var homeView = new LoginView();
            homeView.render();
        });

        app_router.on('route:defaultAction', function(actions) {
            var homeView = new HomeView();
            homeView.render();
        });
        Backbone.history.start();
    };
    return {
        initialize: initialize
    };
});